/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.tool;

import com.funcom.peeler.ExtensionFileFilter;
import com.funcom.peeler.path.RootedFileSystemView;
import com.funcom.peeler.peelable.PeelRoot;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import static com.funcom.peeler.utils.RelativePath.getRelativeFile;

public class Settings extends JFrame {

    private JTabbedPane tabbedPanel;
    private JPanel mainPanel;
    private JButton closeButton;
    private JTextField basePath;
    private JButton basePathChooseButton;
    private JTextField localizerBundle;
    private JButton localizerChooseButton;
    private PeelRoot root;
    private String workingFile;

    public Settings( PeelRoot root ) {
        this.root = root;
        setTitle("Options");
        setSize(400, 300);
        setLocation( 400, 400 );

        setBasePath( System.getProperty("user.dir") + "/" );
        initListeners();
        getContentPane().add(mainPanel);

    }

    private void initListeners() {

        basePathChooseButton.addActionListener( new ActionListener() {

            public void actionPerformed( ActionEvent e ) {

                File rootPath = new File( getBasePath() == null ? Settings.this.root.getLastPath() : getBasePath() );
                JFileChooser chooser = new JFileChooser( rootPath );
                chooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );

                int opt = chooser.showDialog( Settings.this, "Select base path" );
                if ( opt == JFileChooser.APPROVE_OPTION ) {
                    File file = chooser.getSelectedFile();

                    setBasePath( file.getPath() );
                }
            }
        });

        localizerChooseButton.addActionListener( new ActionListener() {

            public void actionPerformed( ActionEvent e ) {

                File rootPath = new File( Settings.this.root.getLastPath() == null ? basePath.getText() : Settings.this.root.getLastPath() );
                JFileChooser chooser = new JFileChooser( Settings.this.root.getLastPath(), new RootedFileSystemView( rootPath ) );
                chooser.setFileFilter( new ExtensionFileFilter(".properties", new String[] { "properties"}) );

                int opt = chooser.showDialog( Settings.this, "Use resource bundle" );

                if ( opt == JFileChooser.APPROVE_OPTION ) {
                    File bundleFile = getRelativeFile( chooser.getSelectedFile(), rootPath );
                    String bundlePath = bundleFile.getPath();
                    bundlePath = bundlePath.substring( 0, bundlePath.lastIndexOf( "_" ) );

                    localizerBundle.setText( bundlePath );
                }
            }
        });

        closeButton.addActionListener( new ActionListener() {

            public void actionPerformed( ActionEvent e ) {

                setVisible( false );
            }
        });

    }

    public String getLocalizerPath() {
        return localizerBundle.getText();
    }

    public void setLocalizerPath( String path ) {
        localizerBundle.setText( path );
    }

    public String getBasePath() {
        return basePath.getText();
    }

    public void setBasePath( String path ) {
        basePath.setText( path );
        System.setProperty( "user.dir", basePath.getText() );
    }

    public void setWorkingFile( String workingFile ) {
        this.workingFile = workingFile;
    }

    public String getWorkingFile() {
        return workingFile;
    }
}
